package com.hawthorn.admin.repository.provider;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.builder.annotation.ProviderMethodResolver;

import java.util.Map;

/**
 * <p>todo user provider sql</p>
 *
 * @version v1.0.1
 * @author: andyten@tom.com | xukaixing@hotmail.com
 * @created: 2021/12/2 3:38 PM
 * @copyright: Copyright (c)  andyten
 */
@Slf4j
public class SysUserSqlProvider implements ProviderMethodResolver
{
  public static String findUserLoginInfo(Page<Map<String, Object>> page, String qc)
  {
    String sql = "select " +
        "login_ip loginIp, " +
        "browser , " +
        "login_division loginDivision, " +
        "login_time loginTime " +
        "from sys_log_login ";
    sql += "where " + qc;
    return sql;
  }

  public static String findUsers(Page<Map<String, Object>> page, String conditions)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" select\n ");
    sql.append(" 	u.id,\n ");
    sql.append(" 	u.account,\n ");
    sql.append(" 	u.user_name userName,\n ");
    sql.append(" 	u.user_sn userSn,\n ");
    sql.append(" 	u.post_station postStation,\n ");
    sql.append(" 	u.mobile,\n ");
    sql.append(" 	u.id_card idCard,\n ");
    sql.append(" 	u.sex,\n ");
    sql.append(" 	u.birth_date birthDate,\n ");
    sql.append(" 	u.email,\n ");
    sql.append(" 	u.theme_type themeType,\n ");
    sql.append(" 	u.remark,\n ");
    sql.append(" 	u.biz_type bizType,\n ");
    sql.append(" 	u.user_level userLevel,\n ");
    sql.append(" 	u.user_type userType,\n ");
    sql.append(" 	u.status,\n ");
    sql.append(" 	u.create_by createBy,\n ");
    sql.append(" 	u.create_time createTime,\n ");
    sql.append(" 	u.dept_code deptCode,\n ");
    sql.append(" 	u.dept_id deptId,\n ");
    sql.append(" 	d.dept_type deptType,\n ");
    sql.append(" 	u.ex_field1 exField1,\n ");
    sql.append(" 	u.ex_field2 exField2,\n ");
    sql.append(" 	(SELECT GROUP_CONCAT(T.label) FROM sys_dict T WHERE INSTR(U.ex_field1,T.dic_code) > 0 AND T.parent = 408100 ) levelName,\n ");
    sql.append(" 	(SELECT GROUP_CONCAT(M.label) FROM sys_dict M WHERE INSTR(U.ex_field2,M.dic_code) > 0 AND M.parent = 408200 ) sanbaoName,\n ");
    sql.append(" 	IFNULL(u.ex_field4,100102) exField4,\n ");
    sql.append(" 	IFNULL(u.ex_field5,100102) exField5,\n ");
    // sql.append(" 	(select a.phone from app_user a where a.phone = u.mobile limit 1) phone,\n ");
    sql.append(" 	group_concat( r.id ) rid,\n ");
    // sql.append(" 	group_concat( r.rcode ) rcode,\n ");
    sql.append(" 	group_concat( r.rname ) rname \n ");
    sql.append(" from\n ");
    sql.append(" 	sys_user u\n ");
    sql.append(" 	left join sys_dept d on d.id = u.dept_id\n ");
    sql.append(" 	left join sys_user_role ur on u.id = ur.user_id\n ");
    sql.append(" 	left join sys_role r on ur.role_id = r.id \n ");
    sql.append(" where");
    sql.append(conditions);
    sql.append(" group by\n ");
    sql.append(" 	u.id \n ");
    return sql.toString();
  }

  public static String findUsersList(Page<Map<String, Object>> page, String conditions)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" select\n ");
    sql.append(" 	u.id,\n ");
    sql.append(" 	u.account,\n ");
    sql.append(" 	u.user_name userName,\n ");
    sql.append(" 	u.user_sn userSn,\n ");
    sql.append(" 	u.post_station postStation,\n ");
    sql.append(" 	u.mobile,\n ");
    sql.append(" 	u.id_card idCard,\n ");
    sql.append(" 	u.sex,\n ");
    sql.append(" 	u.birth_date birthDate,\n ");
    sql.append(" 	u.email,\n ");
    sql.append(" 	u.theme_type themeType,\n ");
    sql.append(" 	u.remark,\n ");
    sql.append(" 	u.biz_type bizType,\n ");
    sql.append(" 	u.user_level userLevel,\n ");
    sql.append(" 	u.user_type userType,\n ");
    sql.append(" 	u.status,\n ");
    sql.append(" 	u.create_by createBy,\n ");
    sql.append(" 	u.create_time createTime,\n ");
    sql.append(" 	u.dept_code deptCode,\n ");
    sql.append(" 	u.dept_id deptId,\n ");
    sql.append(" 	d.dept_type deptType\n ");
    sql.append(" from\n ");
    sql.append(" 	sys_user u\n ");
    sql.append(" 	left join sys_dept d on d.id = u.dept_id\n ");
    sql.append(" where");
    sql.append(conditions);
    return sql.toString();
  }

  public static String findDealerUsers(Page<Map<String, Object>> page, String conditions)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" select\n ");
    sql.append(" 	u.id,\n ");
    sql.append(" 	u.account,\n ");
    sql.append(" 	u.user_name userName,\n ");
    sql.append(" 	u.user_sn userSn,\n ");
    sql.append(" 	u.post_station postStation,\n ");
    sql.append(" 	u.mobile,\n ");
    sql.append(" 	u.id_card idCard,\n ");
    sql.append(" 	u.sex,\n ");
    sql.append(" 	u.birth_date birthDate,\n ");
    sql.append(" 	u.email,\n ");
    sql.append(" 	u.theme_type themeType,\n ");
    sql.append(" 	u.remark,\n ");
    sql.append(" 	u.biz_type bizType,\n ");
    sql.append(" 	u.user_level userLevel,\n ");
    sql.append(" 	u.user_type userType,\n ");
    sql.append(" 	u.status,\n ");
    sql.append(" 	u.create_by createBy,\n ");
    sql.append(" 	u.create_time createTime,\n ");
    sql.append(" 	u.dept_code deptCode,\n ");
    sql.append(" 	u.dept_id deptId,\n ");
    sql.append(" 	d.dept_type deptType,\n ");
    sql.append(" 	IFNULL(u.ex_field4,100102) exField4,\n ");
    sql.append(" 	IFNULL(u.ex_field5,100102) exField5,\n ");
    // sql.append(" 	(select a.phone from app_user a where a.phone = u.mobile limit 1) phone,\n ");
    sql.append(" 	group_concat( r.id ) rid,\n ");
    // sql.append(" 	group_concat( r.rcode ) rcode,\n ");
    sql.append(" 	group_concat( r.rname ) rname \n ");
    sql.append(" from\n ");
    sql.append(" 	sys_user u\n ");
    sql.append(" 	left join sys_dept d on d.id = u.dept_id\n ");
    sql.append(" 	left join sys_user_role ur on u.id = ur.user_id\n ");
    sql.append(" 	left join sys_role r on ur.role_id = r.id \n ");
    sql.append(" where");
    sql.append(" d.level = 100606 and \n");
    sql.append(conditions);
    sql.append(" and not exists(select 1 from sys_user_role r1 where u.id = r1.user_id AND r1.role_code = 'JXS001') \n");
    sql.append(" group by\n ");
    sql.append(" 	u.id \n ");
    return sql.toString();
  }

  /**
   * <p>todo 获取用户详细信息SQL</p>
   * @param userId
   * @return: java.lang.String
   * @date: 2022/6/30 15:09
   * @version: 1.0.1
   * Modification History:
   * Date Author Version Description
   * -----------------------------------------------------------
   */
  public static String selectUserInfoById(String userId)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" select\n ");
    sql.append(" 	u.account,\n ");
    sql.append(" 	u.user_name userName,\n ");
    sql.append(" 	u.sex,\n ");
    sql.append(" 	u.email,\n ");
    sql.append(" 	u.mobile,\n ");
    sql.append(" 	d.dname \n ");
    sql.append(" from\n ");
    sql.append(" 	sys_user u\n ");
    sql.append(" 	left join sys_dept d on u.dept_id = d.id\n ");
    sql.append(" 	where\n ");
    sql.append(" 	u.id = " + userId);
    return sql.toString();
  }

  /**
   * <p>todo 导出用户信息</p>
   * @param headFields
   * @param conditions
   * @return: java.lang.String
   * @date: 2022/7/6 11:19
   * @version: 1.0.1
   */
  public String exportUsers(String headFields, String conditions)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" select\n ");
    if (headFields.contains("r.rcode"))
    {
      headFields = headFields.replace("r.rcode", "group_concat( r.rcode ) rcode");
    }
    if (headFields.contains("r.rname"))
    {
      headFields = headFields.replace("r.rname", "group_concat( r.rname ) rname");
    }
    sql.append(headFields);
    sql.append(" from\n ");
    sql.append(" 	sys_user u\n ");
    sql.append(" 	left join sys_dept d on d.id = u.dept_id\n ");
    sql.append(" 	left join sys_user_role ur on u.id = ur.user_id\n ");
    sql.append(" 	left join sys_role r on ur.role_id = r.id \n ");
    sql.append(" where\n ");
    sql.append(conditions);
    sql.append("  and u.del_flag = 0");
    sql.append(" group by\n ");
    sql.append(" 	u.id\n ");
    sql.append("  order by u.create_time desc \n");
    return sql.toString();
  }

  /**
   * <p>todo 按账号搜索用户</p>
   * @param account
   * @return: java.lang.String
   * @date: 2022/7/7 11:22
   * @version: 1.0.1
   */
  public String findUserByAccount(String account)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" select\n ");
    sql.append(" 	count( id ) \n ");
    sql.append(" from\n ");
    sql.append(" 	sys_user \n ");
    sql.append(" where\n ");
    sql.append(" 	account = " + "'" + account + "'");
    return sql.toString();
  }

  /**
   * <p>todo 判断联系方式是否重复</p>
   * @author: sunwanzhong
  –
   * @created: 2023/3/21 14:37
   * @version v1.0.1
   * @copyright: Copyright (c) 2023 sunwanzhong
   */
  public String findUserByMobile(String mobile)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" select\n ");
    sql.append(" 	count( id ) \n ");
    sql.append(" from\n ");
    sql.append(" 	sys_user \n ");
    sql.append(" where\n ");
    sql.append(" 	mobile = " + "'" + mobile + "'");
    return sql.toString();
  }

  /**
   * <p>todo 判断是否已经存在经销商了</p>
   * @author: sunwanzhong
  –
   * @created: 2023/4/5 14:27
   * @version v1.0.1
   * @copyright: Copyright (c) 2023 sunwanzhong
   */
  public String selectCustomerCount(Long deptId, Long userId)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" select \n ");
    sql.append(" 	count( 1 )  \n ");
    sql.append(" from \n ");
    sql.append(" 	sys_user a \n ");
    sql.append(" 	left join sys_user_role b on a.id = b.user_id  \n ");
    sql.append(" where \n ");
    sql.append(" 	a.dept_id = " + deptId);
    sql.append(" 	and b.role_code = 'JXS001' \n ");
    if (userId != null)
    {
      sql.append(" 	and a.id <> " + userId);
    }
    return sql.toString();
  }

  /**
   * <p>todo 查询经销商下的用户</p>
   * @author: sunwanzhong
  –
   * @created: 2023/4/5 21:46
   * @version v1.0.1
   * @copyright: Copyright (c) 2023 sunwanzhong
   */
  public String selectUserList(Long deptId)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" select \n ");
    sql.append(" 	id  \n ");
    sql.append(" from \n ");
    sql.append(" 	sys_user a \n ");
    sql.append(" where \n ");
    sql.append(" 	a.dept_id = " + deptId);
    return sql.toString();
  }

  public String selectUserInfo(String userId)
  {

    StringBuffer sql = new StringBuffer();
    sql.append(" select \n ");
    sql.append(" 	a.id, \n ");
    sql.append(" 	a.user_name userName, \n ");
    sql.append(" 	a.dept_id deptId, \n ");
    sql.append(" 	b.role_id roleId, \n ");
    sql.append(" 	b.role_code roleCode, \n ");
    sql.append(" 	c.rname  \n ");
    sql.append(" from \n ");
    sql.append(" 	sys_user a, \n ");
    sql.append(" 	sys_user_role b, \n ");
    sql.append(" 	sys_role c  \n ");
    sql.append(" where \n ");
    sql.append(" 	a.id = 1007  \n ");
    sql.append(" 	and a.id = b.user_id  \n ");
    sql.append(" 	and b.role_id = c.id \n ");
    return sql.toString();
  }

}
